package router

import (
	"fmt"

	"github.com/gin-gonic/gin"

	"aicloud.cn/aicloud-operate/cache_type"
	"aicloud.cn/aicloud-operate/config/operations"
	"aicloud.cn/aicloud-operate/pkg/httpx"
	"aicloud.cn/aicloud-operate/pkg/util"
	_const "aicloud.cn/aicloud-operate/pkg/util/const"
)

type Router struct {
	HTTP        *httpx.Config
	UserCache   *cache_type.UserCache
	Operations  operations.Operation
	ShowCaptcha bool // 登录时，是否要求输入验证码

	//todo
}

func (rt *Router) Config(r *gin.Engine) {
	// 免验证路由
	//unAuthGroup := r.Group("/operate")
}

func NewRouter(httpConfig *httpx.Config, userCache *cache_type.UserCache, ops operations.Operation) *Router {
	return &Router{
		HTTP:       httpConfig,
		UserCache:  userCache,
		Operations: ops,
	}
}

func InitializeRouter() {
	listenAddress := fmt.Sprintf(":%s", util.GetEnvOrDefault("PORT", "8080"))
	router := gin.New()
	router.Use(gin.Recovery())
	// todo 详细配置从文件中读取
	rt := NewRouter(&httpx.Config{JWTAuth: httpx.JWTAuth{
		SigningKey: _const.TOKEN_SIGN_KEY,
	}}, cache_type.NewUserCache(), operations.Operations)
	rt.Config(router)
	router.Run(listenAddress)
}
